
/*
  @Author: 于辉
  @Time: 2020-11
  @Tittle: rolePowers
  @Description: 用户列表
*/
layui.config({
    base: "../../../js/plugins/"
}).extend({
    "treeSelect": "treeSelect"
})
var form;
layui.use(['form','layer','jquery',"table","tree","treeSelect"],function(){
    var table = layui.table;
    var layer = layui.layer;
    var treeSelect= layui.treeSelect;
         form = layui.form;
    //渲染数据列表
    var renderTable = function (param){
        table.render({
            id: 'userID'
            ,elem: '#dataTable'
            ,url:getServerPath("GETPOWERLIST")
            ,method: 'post'
            ,contentType: 'application/json'
            ,height: 'full-150'
            ,even: true //开启隔行背景
            ,headers:{SYSTOKEN: getSettings("token")}
            ,where: {
                "Name": $("#search_input").val(),
                "orderBy": "createTime",
                "sort": "descending"
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                console.log(res)
                return {
                    "code": res.statusCode == 200?0:res.statusCode, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.totalCount, //解析数据长度
                    "data": res.data.dataSource //解析数据列表
                };
            }
            ,cols: [[
                ,{field:'id', width:80, title: 'ID',hide:true, sort: true}
                ,{field:'name', width:300, title: '权限标识',align:"center"}
                ,{field:'page', width:300, title: '系统页面',align:"center"}
                ,{field:'description', width:200, title: '权限描述',align:"center"}
                ,{field:'remark', title: '备注',align:"center"}
                ,{field:'', width:200, title: '操作' ,align:"center" , templet:function(row){
                        var html = '<button type="button" class="layui-btn layui-btn-sm" onclick="editInfo(\''+JSON.stringify(row).replace(/"/g, '&quot;') +'\')"><i class="layui-icon layui-icon-edit"></i>编辑</button>';

                        html += '<button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="deleteInfo(\''+row.id+'\')"><i class="iconfont gkb-shanchu"></i>删除</button>';

                        return html;
                    }}
            ]]
            ,page: true
        });
        $('#page').css('text-align', 'right');  // 分页右对齐
        $('.layui-table-page').css('text-align', 'right');  // 数据表格分页右对齐
    }
    renderTable();
    //搜索
    layui.$("#btn_search").on("click", function () {
        renderTable( );
    })
    //添加权限
    layui.$('#btn_add').on('click', function(){
        //页面层-自定义
        layindex = layer.open({
            type: 1,
            title: "权限信息",
            closeBtn: 1,
            shadeClose: true,
            area: ['603px', '450px'],
            content: $("#detailInfo")
        });
    });

    //加载详情页 树形选择框
    var reloadTreeSelect = function(data){
        // 初始化下拉选择器
        treeSelect.render({
            // css选择器，推荐使用id
            elem: '#page',
            data:getServerPath("GETMENULISTNOAUTH"),
            type: 'post',
            contentType: 'application/json',
            headers:{SYSTOKEN: getSettings("token")},
            // 返回数据中主键的属性名称，默认值为id
            key: {
                id: 'id',
            },
            // 节点点击回调函数
            click: function (d) {
                console.log(d);
                form.val('info',{page:d.current.name} );
            }
        });
    }
    reloadTreeSelect();

    //保存方法
    form.on('submit(btn_save)', function(data) {
        data = data.field;
        console.log(data)
        ajaxSetting({
            url:  data.id==null || data.id == ''? getServerPath("CREATEPOWERS"):getServerPath("UPDATEPOWERS"),
            data:JSON.stringify(data),
            success:function(res){
                renderTable();
                layer.msg("保存成功!",{icon:6});
                setTimeout(function(){
                    layer.close(layindex)
                },200)
            }
        })
        return false;
    });

});

function editInfo(detailInfo) {
    console.log(detailInfo)
    detailInfo = JSON.parse(detailInfo);
    //页面层-自定义
    layindex = layer.open({
        type: 1,
        title: "菜单信息",
        closeBtn: 1,
        shadeClose: true,
        area: ['603px', '450px'],
        content: $("#detailInfo"),
        success: function (layero, index) {
            form.val('info', detailInfo);
            $("#pageControl .layui-select-title input").val(detailInfo.page)
        }
    });
}
//删除权限信息
function deleteInfo (id){
    layer.confirm('是否确定要删除该权限？', {
        btn: ['删除','取消']
    }, function(){
        ajaxSetting({
            url:getServerPath("DELETEPOWERS"),
            data:{id:id},
            type:'get',
            success:function(res){
                layer.msg("删除成功",{icon:6})
                //执行查询功能
                $("#btn_search").trigger("click");
            },
            error:function(res){
            }
        })
    }, function(){
    });
}


